const { $fn, $wx, $http, img } = getApp()
Page({
    data: {
        img,
        data:{},
        user: {},
        shortCh:['京','沪','津','渝','冀','豫','云','辽','黑','湘','皖','鲁','新','苏','浙','赣','鄂','桂','甘','晋','蒙','陕','吉','闽','贵','粤','川','青','藏','琼','宁'],
        shortEn:['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'],
        en:'A',
        ch:'京',

        carNo: '',
        translateSpace: 0,
        inputType: 1, // 车牌输入类型，1简称，2数字或者字母,
        showPlateInput: false,
        reid:''
    },
    onLoad({q}){
        if(q){
          const url = decodeURIComponent(q)
          const arr = url.split('=')
          this.setData({ reid: arr[1] })
        }
    },
    onSumit(e){
        const { vno, ch, en, promotion_user, real_name, phone } = e.detail.value
        const param = { vno: ch+en+vno, promotion_user, real_name, phone }
       
        // if(!$fn.isValid(vno)) return $wx.toast('请输入车牌号')
        if(!( /^[1][3,4,5,7,8,9][0-9]{9}$/.test(phone) )) return $wx.toast('请输入正确手机号')
        $http.submit(this,'car.createCar',{ param }).then(data=>{
            $wx.toastSuccess('登记成功')
            $wx.pushTab('/pages/index/index')
        })
    },
    onSelectCh(e){
        this.setData({ ch: this.data.shortCh[e.detail.value] })
    },
    onSelectEn(e){
        this.setData({ en: this.data.shortEn[e.detail.value] })
    },
    // 车牌选择
    /* 用于点击弹出键盘输入，space为键盘弹出后向上拉取的距离 */
  handleClick(e) {
    /* 150为键盘的高度 */
    let space = -(e.currentTarget.offsetTop - 150);
    /* regExp用于判断当前已输入的车牌号是否是中文，并让键盘显示中文还是英文输入 */
    let regExp = /^[\u4e00-\u9fa5]+/;
    let inputType = 1;
    if(regExp.test(this.data.carNo)) {
      inputType = 2;
    }
 
    this.setData({
      translateSpace: space,
      showPlateInput: true,
      inputType
    })
  },
  /* 键盘输入操作 */
  handlePlateChange(e) {
    let value = e.detail.value;
    let type = e.detail.type;
    let carNo = this.data.carNo;
    carNo += value;
 
    if(type == 1) {
      this.setData({
        inputType: 2
      })
    }
    this.setData({
      carNo
    })
  },
  /* 点击键盘上的确定 */
  handlePlateConfirm() {
    /* isCarPlate用于判断输入的车牌号是否符合规范 */
    if (!this.isCarPlate(this.data.carNo)) {
      wx.showToast({
        title: '请输入正确的车牌号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      translateSpace: 0,
      showPlateInput: false,
      inputType: 1
    })
  },
  /* 用于键盘输入删除 */
  handlePlateDelete(e) {
    let carNo = this.data.carNo;
    carNo = carNo.substring(0, carNo.length - 1);
    if(carNo.length == 0) {
      this.setData({
        inputType: 1
      })
    }
    this.setData({
      carNo,
    })
  },
  /* 判断车牌号 */
  isCarPlate(value) {
    return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(value);
  }

})